home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1995 August: Tool Chest / Dev.CD Aug 95 TC / Dev.CD Aug 95 TC.toast / Tool Chest / Development Tools & Languages / Dylan Related / Marlais / Marlais 0.5.9-portable sources / gc / test_cpp.cc < prev    next >
Encoding:
C/C++ Source or Header  |  1995-03-15  |  5.1 KB  |  171 lines  |  [TEXT/ttxt]

  1. /****************************************************************************
  2. Copyright (c) 1994 by Xerox Corporation.  All rights reserved.
  3.  
  4. THIS MATERIAL IS PROVIDED AS IS, WITH ABSOLUTELY NO WARRANTY EXPRESSED
  5. OR IMPLIED.  ANY USE IS AT YOUR OWN RISK.
  6.  
  7. Permission is hereby granted to use or copy this program for any
  8. purpose, provided the above notices are retained on all copies.
  9. Permission to modify the code and to distribute modified code is
  10. granted, provided the above notices are retained, and a notice that
  11. the code was modified is included with the above copyright notice.
  12. ****************************************************************************
  13. Last modified on Wed Jan  4 16:35:11 PST 1995 by ellis
  14.      modified on December 20, 1994 7:27 pm PST by boehm
  15.  
  16. usage: test_cpp number-of-iterations
  17.  
  18. This program tries to test the specific C++ functionality provided by
  19. gc_c++.h that isn't tested by the more general test routines of the
  20. collector.
  21.  
  22. A recommended value for number-of-iterations is 10, which will take a
  23. few minutes to complete.
  24.  
  25. ***************************************************************************/
  26.  
  27. #include "gc_cpp.h"
  28. #include <assert.h>
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31.  
  32. class A {public:
  33.     /* An uncollectable class. */
  34.  
  35.     A( int iArg ): i( iArg ) {}
  36.     void Test( int iArg ) {
  37.         assert( i == iArg );} 
  38.     int i;};
  39.  
  40.  
  41. class B: public gc, public A {public:
  42.     /* A collectable class. */
  43.  
  44.     B( int j ): A( j ) {}
  45.     ~B() {
  46.         assert( deleting );}
  47.     static void Deleting( int on ) {
  48.         deleting = on;}
  49.     static int deleting;};
  50.  
  51. int B::deleting = 0;
  52.  
  53.  
  54. class C: public gc_cleanup, public A {public:
  55.     /* A collectable class with cleanup and virtual multiple inheritance. */
  56.  
  57.     C( int levelArg ): A( levelArg ), level( levelArg ) {
  58.         nAllocated++;
  59.         if (level > 0) {
  60.             left = new C( level - 1 );
  61.             right = new C( level - 1 );}
  62.         else {
  63.             left = right = 0;}}
  64.     ~C() {
  65.         this->A::Test( level );
  66.         nFreed++;
  67.         assert( level == 0 ? 
  68.                    left == 0 && right == 0 :
  69.                    level == left->level + 1 && level == right->level + 1 );
  70.         left = right = 0;
  71.         level = -123456;}
  72.     static void Test() {
  73.         assert( nFreed <= nAllocated && nFreed >= .8 * nAllocated );}
  74.  
  75.     static int nFreed;
  76.     static int nAllocated;
  77.     int level;
  78.     C* left;
  79.     C* right;};
  80.  
  81. int C::nFreed = 0;
  82. int C::nAllocated = 0;
  83.  
  84.  
  85. class D: public gc {public:
  86.     /* A collectable class with a static member function to be used as
  87.     an explicit clean-up function supplied to ::new. */
  88.  
  89.     D( int iArg ): i( iArg ) {
  90.         nAllocated++;}
  91.     static void CleanUp( void* obj, void* data ) {
  92.         D* self = (D*) obj;
  93.         nFreed++;
  94.         assert( self->i == (int) data );}
  95.     static void Test() {
  96.         assert( nFreed >= .8 * nAllocated );}
  97.        
  98.     int i;
  99.     static int nFreed;
  100.     static int nAllocated;};
  101.  
  102. int D::nFreed = 0;
  103. int D::nAllocated = 0;
  104.  
  105.  
  106. long Disguise( void* p ) {
  107.     return ~ (long) p;}
  108.  
  109. void* Undisguise( long i ) {
  110.     return (void*) ~ i;}
  111.  
  112.  
  113. int main( int argc, char* argv[] ) {
  114.     int i, iters, n;
  115.  
  116.     if (argc != 2 || (0 >= (n = atoi( argv[ 1 ] )))) {
  117.         fprintf( stderr, "usage: test_cpp number-of-iterations\n" );
  118.         exit( 1 );}
  119.         
  120.     for (iters = 1; iters <= n; iters++) {
  121.         printf( "Starting iteration %d\n", iters );
  122.  
  123.             /* Allocate some uncollectable As and disguise their pointers.
  124.             Later we'll check to see if the objects are still there.  We're
  125.             checking to make sure these objects really are uncollectable. */
  126.         long as[ 1000 ];
  127.         long bs[ 1000 ];
  128.         for (i = 0; i < 1000; i++) {
  129.             as[ i ] = Disguise( new (NoGC) A( i ) );
  130.             bs[ i ] = Disguise( new (NoGC) B( i ) );}
  131.  
  132.             /* Allocate a fair number of finalizable Cs and Ds.  Later we'll
  133.             check to make sure they've gone away. */
  134.         for (i = 0; i < 1000; i++) {
  135.             C* c = new C( 2 );
  136.             C c1( 2 );           /* stack allocation should work too */
  137.             D* d = ::new (GC, D::CleanUp, (void*) i) D( i );
  138.             if (0 == i % 10) delete c;}
  139.  
  140.             /* Allocate a very large number of collectable As and Bs and
  141.             drop the references to them immediately, forcing many
  142.             collections. */
  143.         for (i = 0; i < 1000000; i++) {
  144.             A* a = new (GC) A( i );
  145.             B* b = new B( i );
  146.             b = new (GC) B( i );
  147.             if (0 == i % 10) {
  148.                 B::Deleting( 1 );
  149.                 delete b;
  150.                 B::Deleting( 0 );}}
  151.  
  152.             /* Make sure the uncollectable As and Bs are still there. */
  153.         for (i = 0; i < 1000; i++) {
  154.             A* a = (A*) Undisguise( as[ i ] );
  155.             B* b = (B*) Undisguise( bs[ i ] );
  156.             a->Test( i );
  157.             delete a;
  158.             b->Test( i );
  159.             B::Deleting( 1 );
  160.             delete b;
  161.             B::Deleting( 0 );}
  162.  
  163.             /* Make sure most of the finalizable Cs and Ds have gone away. */
  164.         C::Test();
  165.         D::Test();}
  166.  
  167.     printf( "The test appears to have succeeded.\n" );
  168.     return( 0 );}
  169.     
  170.  
  171.